/*
 * Copyright 2008-2009 Daniel Cachapa <cachapa@gmail.com>
 * 
 * This program is distributed under the terms of the GNU General Public Licence Version 3
 * The licence can be read in its entirety in the LICENSE.txt file accompaning this source code,
 * or at: http://www.gnu.org/copyleft/gpl.html
 * 
 * This file is part of WeightWatch.
 *
 * WeightWatch is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 *
 * WeightWatch is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with the WeightWatch source code. If not, see
 * <http://www.gnu.org/licenses/>.
 */

package net.cachapa.weightwatch.ui;

import com.sun.lwuit.Button;
import com.sun.lwuit.Container;
import com.sun.lwuit.Image;
import com.sun.lwuit.Label;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.events.DataChangedListener;
import com.sun.lwuit.layouts.BoxLayout;
import net.cachapa.weightwatch.ui.components.NumericField;
import net.cachapa.weightwatch.util.L;

public class UserPreferencesContainer extends Container implements PreferencesContainer, ActionListener, DataChangedListener {
	private Application application;
	private String name;
	private Image icon;
	private NumericField userHeightField;
	private Button setPlanButton;
	private boolean userHeightChanged = false;
	
	public UserPreferencesContainer(Application application) {
		this.application = application;
		this.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
		this.name = L.l("pref_tab_user");
		try {
			this.icon = Image.createImage("/user.png");
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		
		Container heightContainer = new Container(new BoxLayout(BoxLayout.X_AXIS));
		this.userHeightField = new NumericField(10);
		this.userHeightField.setUpDownValueChange(0.01);
		this.userHeightField.addDataChangeListener(this);
		heightContainer.addComponent(new Label("user_height"));
		heightContainer.addComponent(userHeightField);
		this.addComponent(heightContainer);
		
		this.setPlanButton = new Button("user_setplan");
		this.setPlanButton.setAlignment(CENTER);
		this.setPlanButton.addActionListener(this);
		this.addComponent(this.setPlanButton);
	}
	
	public void actionPerformed(ActionEvent ae) {
		saveUserHeight();
		if (ae.getSource() == setPlanButton) {
			application.performAction(Application.SHOW_PLAN_FORM);
		}
	}

	public void show() {
		this.userHeightField.setText(application.getWeightWatch().getPreferences().getUserHeight()+"");
	}

	public void dataChanged(int type, int index) {
		userHeightChanged = true;
	}
	
	public void saveUserHeight() {
		if (userHeightChanged) {
			application.getWeightWatch().getPreferences().setUserHeight(userHeightField.getValue());
		}
		userHeightChanged = false;
	}
	
	public String getName() {
		return name;
	}

	public Image getIcon() {
		return icon;
	}
}
